from time import sleep

from selenium.webdriver.common.by import By

from base.base import Base

url = 'http://192.168.8.222/admin.php'


class Phone2(Base):
    def login(self):
        self.save_cookie('../data/w_warehouse_cookies.json')
        sleep(20)

    def login_admin(self):
        self.set_cookie('../data/w_warehouse_cookies.json')
        self.driver.get(url)
        self.click((By.XPATH, '//*[@id="admin-offcanvas"]/div/ul/li[11]/a'))
        # 点击首页导航，进入iframe
        self.click((By.XPATH, '//*[@id="power-menu-319"]/li[3]/a'))
        self.go_iframe((By.ID, "ifcontent"))

    def add_user(self, name, value, describe, order):
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/a[1]'))
        # 用户名称
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/input'), name)
        # 所属平台
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div/div/ul/li[4]'))
        # 事件类型
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/div/div/ul/li[2]'))
        # 事件值
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[4]/input'), value)
        # 描述
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[5]/input'), describe)
        sleep(30)
        # 顺序
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[7]/input'), order)
        # 保存
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[9]/button'))
        sleep(1)
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))

    def delete_user(self):
        # 勾选
        self.click((By.CSS_SELECTOR, 'td:nth-child(1) > div > label'))
        # 删除
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/button[1]'))
        # 确认
        self.click((By.CSS_SELECTOR, 'div.am-modal-footer > span:nth-child(2)'))
        sleep(1)
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))

    def reset_search_user(self, name):
        # 输入名称
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[2]/input'), name)
        # 搜索
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/button[2]'))
        sleep(2)
        # 重置
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/a[2]'))

    def sidebar_user(self, name, value, describe, order):
        # 详情
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > button.am-btn.am-btn-default.am-btn-xs.am-radius.am-btn-block.submit-popup'))
        # 退出详情
        sleep(1)
        self.click((By.XPATH, '//div[starts-with(@id,"am-modal-")]/div/span'))
        # 编辑
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > a'))
        # 名称
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/input'), name)
        # 所属平台
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div/div/ul/li[4]'))
        # 事件类型
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/div/div/ul/li[2]'))
        # 事件值
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[4]/input'), value)
        # 描述
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[4]/input'), describe)
        # 顺序
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[4]/input'), order)
        # 保存
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[9]/button'))
        # 删除
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > button.am-btn.am-btn-danger.am-btn-xs.am-radius.am-btn-block.submit-delete'))
        self.click((By.CSS_SELECTOR, 'div.am-modal-footer > span:nth-child(2)'))
        sleep(1)
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))


if __name__ == '__main__':
    p = Phone2()
    p.login_admin()
    # p.add_user('12', '25', '456', '82')
    # p.delete_user()
    # p.reset_search_user('我的收藏')
    p.sidebar_user('12', '34', '56', '2')
